package com.codi.api.controller;

import com.codi.api.security.UserUtil;
import com.codi.base.exception.BaseAppException;
import com.codi.base.log.annotation.Loggable;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.resultModel.RecommendResult;
import com.codi.bus.core.service.LookupService;
import com.codi.bus.core.service.PaymentService;
import com.codi.bus.core.service.QueryService;
import com.codi.bus.core.service.model.FundModel;
import com.codi.bus.core.service.model.TradeLimitModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 模块名
 *
 * @author spy
 * @version 1.0 2017-12-01 13:34
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/pay")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private QueryService queryService;
    @Resource
    private LookupService lookupService;

    @GetMapping("/list")
    @Loggable
    public BaseResult getPayMethods() throws BaseAppException {
        return paymentService.getPayMethods(UserUtil.getLoginUserId());
    }

    @GetMapping("/wallet/list")
    @Loggable
    public BaseResult getPayMethodsForWallet() {
        return paymentService.getPayMethodsForWallet(UserUtil.getLoginUserId());
    }

    @GetMapping("/portfolio/list")
    @Loggable
    public BaseResult getPayMethodsForPortfolio() {
        return paymentService.getPayMethodsForPortfolio(UserUtil.getLoginUserId());
    }

    /**
     * 赎回转购 瑞富宝 推荐
     *
     * @param balance
     * @return
     */
    @GetMapping("/recommend")
    @Loggable
    public BaseResult getRecommend(String balance) {
        BigDecimal banlance2 = new BigDecimal(balance);

        RecommendResult recommendResult = new RecommendResult();
        FundModel fundModelE = queryService.queryFund(GlobalConstant.T0_FUND_CHANGCHENG_E);
        FundModel fundModelA = queryService.queryFund(GlobalConstant.T0_FUND_JINYING2_A);

        if (banlance2.compareTo(new BigDecimal(GlobalConstant.WUBAIWAN)) >= 0 && fundModelE.getCanBuy() || !fundModelA.getCanBuy()) {
            recommendResult.setFundCode(GlobalConstant.T0_FUND_CHANGCHENG_E);
            recommendResult.setFundName(GlobalConstant.T0_FUND_CHANGCHENG_E_NAME);
            List<TradeLimitModel> tradeLimitModels = queryService.queryTradeLimits(GlobalConstant.T0_FUND_CHANGCHENG_E, true);
            // 最小申购
            if (tradeLimitModels != null) {
                for (TradeLimitModel model : tradeLimitModels) {
                    if ("022".equals(model.getFundBusinCode())) {
                        recommendResult.setMinPurchase(model.getMinValue());
                    }
                }
            }
        } else {
            recommendResult.setFundCode(GlobalConstant.T0_FUND_JINYING2_A);
            recommendResult.setFundName(GlobalConstant.T0_FUND_JINYING2_A_NAME);

//            ShareTypeModel shareTypeModel = queryService.getShareType(GlobalConstant.T0_FUND_JINYING2_A);


            // 最低起购金额由后台控制，不走恒生
//            Lookup lookup = lookupService.getLookup(RuleConstant.RULE_LOOKUP_TYPE_T0FUND, recommendResult.getFundCode());
//
//            if (lookup != null) {
//                String ruleLimitValue = lookup.getRuleLimitValue();
//
//                if (com.codi.bus.util.LookupUtil.isJSONObject(ruleLimitValue)) {
//                    JSONObject father = JSONObject.parseObject(ruleLimitValue);
//                    JSONObject child = null;
//                    child = father.getJSONObject("user");
//                    if (child != null) {
//                        String minMoney = child.getString("minMoney");
//                        String reminder = child.getString("reminder");
//                        recommendResult.setMinPurchase(new BigDecimal(minMoney));
//                    }
//                }
//            }


            List<TradeLimitModel> tradeLimitModels = queryService.queryTradeLimits(GlobalConstant.T0_FUND_JINYING2_A, true);
            // 最小申购
            if (tradeLimitModels != null) {
                for (TradeLimitModel model : tradeLimitModels) {
                    if ("022".equals(model.getFundBusinCode())) {
                        recommendResult.setMinPurchase(model.getMinValue());
                    }
                }
            }
        }
        return new BaseResult().setResult(recommendResult);
    }
}
